package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.entity.Invoice;
import com.kaizeli.website.pojo.dto.InvoiceDTO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.InvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 开票单控制器
 */
@Tag(name = "开票管理", description = "开票相关接口")
@RestController
@RequestMapping("/api/invoice")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    @Operation(summary = "根据验收单ID查询开票单列表")
    @GetMapping("/acceptance/{acceptanceId}")
    public Result<List<InvoiceDTO>> getInvoicesByAcceptanceId(
            @Parameter(description = "验收单ID") @PathVariable Integer acceptanceId) {
        return invoiceService.getInvoicesByAcceptanceId(acceptanceId);
    }

    @Operation(summary = "根据项目ID查询开票单列表")
    @GetMapping("/project/{projectId}")
    public Result<List<InvoiceDTO>> getInvoicesByProjectId(
            @Parameter(description = "项目ID") @PathVariable Long projectId) {
        return invoiceService.getInvoicesByProjectId(projectId);
    }

    @Operation(summary = "根据开票状态查询开票单列表")
    @GetMapping("/status/{invoiceStatus}")
    public Result<List<InvoiceDTO>> getInvoicesByInvoiceStatus(
            @Parameter(description = "开票状态") @PathVariable String invoiceStatus) {
        return invoiceService.getInvoicesByInvoiceStatus(invoiceStatus);
    }

    @Operation(summary = "根据ID查询开票单详情")
    @GetMapping("/{id}")
    public Result<InvoiceDTO> getInvoiceById(
            @Parameter(description = "开票单ID") @PathVariable Integer id) {
        return invoiceService.getInvoiceById(id);
    }

    @Operation(summary = "创建开票单")
    @PostMapping
    public Result<Invoice> createInvoice(@RequestBody Invoice invoice) {
        return invoiceService.createInvoice(invoice);
    }

    @Operation(summary = "更新开票单")
    @PutMapping("/{id}")
    public Result<Void> updateInvoice(
            @Parameter(description = "开票单ID") @PathVariable Integer id,
            @RequestBody Invoice invoice) {
        return invoiceService.updateInvoice(id, invoice);
    }

    @Operation(summary = "更新开票状态")
    @PutMapping("/{id}/status")
    public Result<Void> updateInvoiceStatus(
            @Parameter(description = "开票单ID") @PathVariable Integer id,
            @Parameter(description = "开票状态") @RequestParam String invoiceStatus) {
        return invoiceService.updateInvoiceStatus(id, invoiceStatus);
    }

    @Operation(summary = "计算项目总开票金额")
    @GetMapping("/project/{projectId}/total-amount")
    public Result<BigDecimal> calculateTotalInvoiceAmount(
            @Parameter(description = "项目ID") @PathVariable Long projectId) {
        return invoiceService.calculateTotalInvoiceAmount(projectId);
    }

    @Operation(summary = "根据验收单自动创建开票单")
    @PostMapping("/from-acceptance/{acceptanceId}")
    public Result<Invoice> createInvoiceFromAcceptance(
            @Parameter(description = "验收单ID") @PathVariable Integer acceptanceId) {
        return invoiceService.createInvoiceFromAcceptance(acceptanceId);
    }

    @Operation(summary = "删除开票单")
    @DeleteMapping("/{id}")
    public Result<Void> deleteInvoice(
            @Parameter(description = "开票单ID") @PathVariable Integer id) {
        return invoiceService.deleteInvoice(id);
    }
}

